package com.example.newbilityblog.blog.service;

import com.example.newbilityblog.blog.domain.command.CreateBlogCommand;
import com.example.newbilityblog.blog.domain.command.UpdateBlogCommand;
import com.example.newbilityblog.blog.domain.entity.Blog;
import com.example.newbilityblog.blog.domain.infrastructure.BlogRepository;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class BlogAppService {
    private final BlogRepository blogRepository;

    public BlogAppService(BlogRepository blogRepository) {
        this.blogRepository = blogRepository;
    }

    @Transactional
    public void update(Long id, UpdateBlogCommand command) {
        var blog = blogRepository.findById(id)
                .orElseThrow(RuntimeException::new);

        blog.update(command);
        blogRepository.save(blog);
    }

    @Transactional
    public Long create(CreateBlogCommand command) {
        var blog = new Blog();
        blog.create(command);
        blogRepository.save(blog);
        return blog.getId();
    }

    @Transactional
    public void delete(List<Long> blogIds) {
        blogRepository.delete(blogIds);
    }
}
